﻿using System;
using System.Globalization;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ReportManagement
{
    public partial class ReportContractor : System.Web.UI.Page
    {
        private int _roleId;
        protected string _user;
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
            if (Session["RoleId"] != null)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }

        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if(!IsPostBack)
            ListViewClient.DataBind();
        }
        /// <summary>
        /// Filter search clients
        /// </summary>
        protected void FilterClient()
        {
            var userName = textUserName.Text;
            var email = textEmail.Text;

            _roleId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
            _user = Session["UserName"].ToString();
            if (_roleId == 1)
            {
                var query = ContractorProfileLogic.SearchContractor(userName, email);
                Session["SOURCE_CONTRACTOR_TABLE"] = query;
                ListViewClient.DataSource = query;
            }
            else if(_roleId==4)
            {
                var query = ContractorProfileLogic.SearchContractorByConsultant(_user, userName, email);
                Session["SOURCE_CONTRACTOR_TABLE"] = query;
                ListViewClient.DataSource = query;
            }
            ListViewClient.DataBind();
        }
        private void BindData()
        {
            _roleId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
            _user = Session["UserName"].ToString();
            if (_roleId == 1)
            {
                var result = ContractorProfileLogic.SelectData();
                Session["SOURCE_CONTRACTOR_TABLE"] = result;
                ListViewClient.DataSource = result;
            }
            if(_roleId==4)
            {
                var result = ContractorProfileLogic.GetContractorByConsultant(_user);
                Session["SOURCE_CONTRACTOR_TABLE"] = result;
                ListViewClient.DataSource = result;
            }
            ListViewClient.DataBind();
        }
        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.AllReport;

            var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

            AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
        }

        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            FilterClient();
        }

        protected void ButtonCreateReportClick(object sender, EventArgs e)
        {
            Response.Redirect("ShowReportContractor.aspx");
        }
    }
}
